<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="零部件名称" prop="partname">
        <el-input
          v-model="queryParams.partname"
          placeholder="请输入零部件名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="零部件类型" prop="type">
        <el-input
          v-model="queryParams.type"
          placeholder="请输入零部件类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属供应商" prop="suppliername">
        <el-input
          v-model="queryParams.suppliername"
          placeholder="请输入所属供应商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['part:part:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['part:part:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['part:part:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['part:part:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="partList" label-width="120px" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" label-width="120px" align="center"/>
      <el-table-column label="零部件名称" align="center" prop="partname"/>
      <el-table-column label="零部件类型" align="center" prop="type"/>
      <el-table-column label="零部件种类" align="center" prop="modified"/>
      <el-table-column label="零部件数量" align="center" prop="quantity"/>
      <el-table-column label="所属供应商" align="center"  prop="suppliername"/>
      <el-table-column label="进价" align="center" prop="primeprice"/>
      <el-table-column label="规格" align="center" prop="specification"/>
      <el-table-column label="创建时间" align="center" width="150px" prop="createtime"/>
      <el-table-column label="修改时间" align="center" width="150px" prop="modifytime"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['part:part:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['part:part:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改零部件对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="零部件名称" prop="partname">
          <el-input v-model="form.partname" placeholder="请输入零部件名称"/>
        </el-form-item>
        <el-form-item label="零部件类型" prop="type">
          <el-input v-model="form.type" placeholder="请输入零部件类型"/>
        </el-form-item>
        <el-form-item label="原装件/改装件" prop="modified">
          <el-select v-model="form.modified" placeholder="请选择原装件/改装件">
            <el-option label="原装件" value="原装件"></el-option>
            <el-option label="改装件" value="改装件"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="零部件数量" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入零部件数量"/>
        </el-form-item>
        <el-form-item label="所属供应商" prop="supplierid">
          <el-select v-model="form.supplierid" placeholder="请选择所属供应商">
            <el-option
              v-for="(item,index) in supoptions"
              :key="item.index"
              :label="item.suppliername"
              :value="item.supplierid">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="进价" prop="primeprice">
          <el-input v-model="form.primeprice" placeholder="请输入进价"/>
        </el-form-item>
        <el-form-item label="规格" prop="specification">
          <el-input v-model="form.specification" placeholder="请输入规格"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listPart, getPart, delPart, addPart, updatePart} from "@/api/part/part";
import {getlistSuppliers} from "@/api/supplier/supplier";

export default {
  name: "Part",
  data() {
    return {
      supoptions: [],
      modifyoptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 零部件表格数据
      partList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: null,
        supplierid: null,
        suppliername: null,
        warehouseid: null,
        warehousename: null,
        customerid: null,
        customername: null,
        salespersonid: null,
        salespersonname: null,
        partname: null,
        type: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        quantity: [
          {required: true, message: "零部件数量不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: "零部件状态不能为空", trigger: "blur"}
        ],
        supplierid: [
          {required: true, message: "所属供应商不能为空", trigger: "blur"}
        ],
        warehouseid: [
          {required: true, message: "所在仓库不能为空", trigger: "blur"}
        ],
        customerid: [
          {required: true, message: "买家(客户)不能为空", trigger: "blur"}
        ],
        salespersonid: [
          {required: true, message: "销售员不能为空", trigger: "blur"}
        ],
        modified: [
          {required: true, message: "原装件/改装件不能为空", trigger: "blur"}
        ],
        partname: [
          {required: true, message: "零部件名称不能为空", trigger: "blur"}
        ],
        primeprice: [
          {required: true, message: "进价不能为空", trigger: "blur"}
        ],
        price: [
          {required: true, message: "售价不能为空", trigger: "blur"}
        ],
        specification: [
          {required: true, message: "规格不能为空", trigger: "blur"}
        ],
        type: [
          {required: true, message: "零部件类型不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.listSuppliers();
  },
  methods: {

    listSuppliers() {
      getlistSuppliers().then(response => {
        //console.info(response)
        this.supoptions = response.data;
      })
    },
    /** 查询零部件列表 */
    getList() {
      this.loading = true;
      listPart(this.queryParams).then(response => {
        console.info(response.rows);
        this.partList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        quantity: null,

        partid: null,
        status: null,
        supplierid: null,
        warehouseid: null,
        customerid: null,
        salespersonid: null,
        suppliername: null,
        warehousename: null,
        customername: null,
        salespersonname: null,
        partname: null,
        primeprice: null,
        price: null,
        specification: null,
        type: null,
        createtime: null,
        modifytime: null,
        deleted: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.partid)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加零部件";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const partid = row.partid || this.ids
      getPart(partid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改零部件";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.partid != null) {
            updatePart(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPart(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const partids = row.partid || this.ids;
      this.$modal.confirm('是否确认删除零部件编号为"' + partids + '"的数据项？').then(function () {
        return delPart(partids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('part/part/export', {
        ...this.queryParams
      }, `part_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
